package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.controller.BaseController;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.*;
import com.runfast.paotui.service.*;
import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.util.MyUtil;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.awt.print.Pageable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/paotui/*")
public class PaoTuiController extends BaseController<Order, Integer>{
    @Resource
    private OrderService orderService;//定单状态表
    @Resource
    private RunfastCuserService userService;//用户列表
    @Resource
    private RunfastBusinessService businessService;//商家列表
    @Resource
    private RunfastShopperService shopperService;//骑手列表
    @Resource
    RunfastCuseraddressService runfastCuseraddressService;//用户地址
    @Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @PostMapping("/list") //这个paotui接口好像根据需求用不着了
    public @ResponseBody ModelMap list( Integer userid,Integer a,String orderNo,String goodsDescription, Order order,
    Integer buessid,Integer dirverid) {
        //driverId为骑手ID
        //  a 1为代购 2取送件
        //用户ID ,骑手ID
        //goodsDescription 为要买的东西
        //userid为1
        //orderNo 例如"1234"
       // buessid=1
       // dirverid=1
        ModelMap model=new ModelMap();
        Order orderuser=new Order();//通过用户id查到定单的基本情况


        if(a==1){
            //通过用户id获取用户信息
            RunfastCuser user=userService.get(userid);


           RunfastBusiness business = businessService.findById(buessid);//通过商家ID获得商家信息

           RunfastShopper shopper=shopperService.get(dirverid);


         /*   Double shopdis=MyUtil.getDistance(Double.parseDouble(shopper.getLongitude()), Double.parseDouble(shopper.getLatitude()), Double.parseDouble(business.getLongitude()), Double.parseDouble(business.getLatitude()));

            String s1 = String.valueOf(shopdis);
            String s2 = s1.substring(0, s1.indexOf(".")) + s1.substring(s1.indexOf(".")+1);
            int i = Integer.parseInt(s2);*/
      //      model.put("shopdis", shopdis);///骑手到商家距离
    //        returnMap.put("disShop", MyUtil.getDistance( Double.parseDouble(gr.getBusinessaddresslat()),  Double.parseDouble(gr.getBusinessaddresslng()), Double.parseDouble(sp.getLatitude()), Double.parseDouble(sp.getLongitude())));//商户与骑手之间的距离
       //     Double buseenessis= MyUtil.getDistance( Double.parseDouble(user.getUseraddresslat()),  Double.parseDouble(gr.getUseraddresslng()), Double.parseDouble(sp.getLatitude()), Double.parseDouble(sp.getLongitude())));//用户与骑手之间的距离
           RunfastCuseraddress runfastCuseraddress=runfastCuseraddressService.findById(userid);
                //create_time, update_time,amount_payable,goods_description, order_no, STATUS, cuser_id ,delivery_cost,to_id,TYPE,distance,delivery_duration,user_lng
            // ,user_lat,agent_id 这些属性都是不能为空属性，非得要
            orderuser.setCuserId(user.getId());//设置用户ID
             //   orderuser.setDistance();
                orderuser.setGoodsDescription(goodsDescription);//要买的东西

                orderuser.setToId(runfastCuseraddress.getId());//用户地址，全都是runfastCuseraddress.getId()  145307
           //     orderuser.setType(Order.Type.DAIGOU);//订单类型

                orderuser.setAmountPayable(500);//应支付金额,商家配送费+打包费business.getBusshowps().intValue()+business.getPacking().intValue()
                orderuser.setGoodsDescription(goodsDescription);//商品描术
                orderuser.setDeliveryCost(20);//配送费busshowps 转为int  business.getBusshowps().intValue()

                orderuser.setDistance(200);//距离是骑手到商家的距离,应该是用户到商家的距离
                orderuser.setDeliveryDuration(30);//大约配送时间distributiontime   business.getDistributiontime()
                 orderuser.setAgentId(shopper.getAgentId());//代理商ID
                orderuser.setUserLng(500.00);//设置用户的纬度,后期在缓存里获取
                 orderuser.setUserLat(500.00);//设置经度后期在缓存里获取
         //       orderuser.setStatus(Order.Status.CREATED);//用户已下单
                orderuser.setOrderNo(orderNo);//设置定单号
                orderuser.setCreateTime(new Date());//设置定单时间
                orderuser.setUpdateTime(new Date());
//            //设置更新时间
            orderService.insertSelective(orderuser);//保存
            Order orderuser1=orderService.findByUseridAndCode(userid,orderNo);//测试阶段用
                model.put("order",orderuser1);//测试阶段用
                model.put("msg","下单成功");
                //下单成功后，把order的定单转存到外卖定单列表中，以外卖定单的形式进行


/*  /paotui/list
            if(driverId!=null){

                List<Order> orders=orderService.findByShopperidAndStatus(driverId,status);//status为枚举
                model.put("list",orders);
            }*/
        }
        if(a==2){

                Order orderuser2=orderService.findByUseridAndCode(userid,orderNo);//通过用户id查到定单的基本情况
                orderuser2.setFromLat(order.getFromLat());//物品地址

         // 立即取件  优惠券 更多服务 这三个还没有设置
                    orderuser2.setToId(order.getToId());//目的地
                    orderuser2.setGoodsWeight(order.getGoodsWeight());//物品类型
                orderuser2.setInsurance(order.getInsurance());//货损险
                orderuser2.setTip(order.getTip());//小费
                orderuser2.setRemark(order.getRemark());//备注
                orderuser2.setDistance(order.getDistance());//路程
                orderuser2.setDeliveryDuration(order.getDeliveryDuration());//预计配送时间
                orderuser2.setAmountPayable(order.getAmountPayable());//应支付费用
        //        orderuser2.setStatus(Order.Status.PAID);//更改状态.
                orderuser2.setUpdateTime(new Date());
                //接收过来的属性还没有添加
           //     orderService.updateStatusAndTime(orderuser2);
               orderService.updateByPrimaryKeySelective(orderuser2);//更新订单信息
            Order orderuser3=orderService.findByUseridAndCode(userid,orderNo);//测试阶段用
                //根据骑手定单号 ,获得定单信息
             //   Order order=orderService.findOrderCode();
            model.put("order",orderuser3);//测试阶段用

                model.put("sucess","等待取件");
            RunfastGoodsSellRecord record=new RunfastGoodsSellRecord();

            record.setStatus(1);//设置客户已付款
            record.setOrderCode(orderNo);//设置定单号
            record.setCreateTime(orderuser3.getCreateTime());//设置定单时间
            record.setUserAddressId(orderuser3.getToId());//用户地址
            record.setTotalpay(BigDecimal.valueOf((int)orderuser3.getAmountPayable()));//应付金额
            record.setShowps(BigDecimal.valueOf((int)orderuser3.getDeliveryCost()));//配送费
            record.setDistance(orderuser3.getDistance()+" ");//距离是骑手到商家的距离,应该是用户到商家的距离
            record.setDistributionTime(orderuser3.getDeliveryDuration()); //大约配送时间distributiontime   business.getDistributiontime()
            record.setAgentId(orderuser3.getAgentId());//代理商ID
            record.setUserAddressLng(String.valueOf(orderuser3.getUserLng()));//获得经度
            record.setUserAddressLat(String.valueOf(orderuser3.getUserLat()));//获得纬度
            record.setContent(orderuser3.getRemark());////备注
            record.setUserId(orderuser3.getCuserId());//用户ID
            record.setGoodsSellName(goodsDescription);////要买的东西
            record.setAgree(9);//0为反对 1 为同意 9 为跑腿定单
                runfastGoodsSellRecordService.insert(record);
                RunfastGoodsSellRecord sellRecord=runfastGoodsSellRecordService.findByOrderCode(orderNo);//测试用
                model.put("sellRecord", sellRecord);
                model.put("sucess",true);


        }

        return  model;
    }

      /*  RunfastShopper currentDriver = SessionContext.getCurrentDriver();
        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andDriverIdEqualTo(currentDriver.getId());
        List<Order> orders = orderService.selectByExampleWithPageable(orderExample, pageable);
        return Result.ok("", orders);*/

}
